<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace app\adminapi\controller\v1\passport;

use app\adminapi\services\notify\auth\GiteeAuthOAuthNotify;
use app\adminapi\services\passport\OAuthService;
use kitego\base\BaseController;
use kitego\extend\login\admin\AdminLoginProvider;
use kitego\extend\login\admin\enum\AdminLoginEnum;
use kitego\services\common\CacheService;
use oauth\enum\OAuthEnum;
use oauth\OAuthProvider;
use think\App;

class OAuthController extends BaseController
{
    public array $safeAction = ['all'];

    private OAuthService $service;

    public function __construct(App $app, OAuthService $service)
    {
        parent::__construct($app);

        $this->service = $service;
    }

    /**
     * Gitee授权地址
     */
    public function giteeRedirect()
    {
        $oauthProvider = app()->make(OAuthProvider::class, [OAuthEnum::GITEE])->getStrategy();

        return success(['uri' => $oauthProvider->getAuthorizationUri()]);
    }

    /**
     * 处理gitee回调
     */
    public function giteeCallback()
    {
        if (!isset($this->params['code'])) exception('code异常');

        return app()->make(AdminLoginProvider::class, [AdminLoginEnum::GITEE])->getStrategy()->login($this->params);
    }
}